/**
 * Created by Administrator on 2017/3/27 0027.
 */
PageCircle={
    Create:function(){
        var bool_handShow;

        var pc={};
        var touchX=0;


        var cylinderG=new THREE.CylinderGeometry(400*0.95,400*0.95,130*0.95,32,1,true);
        var cylinderM=new THREE.MeshBasicMaterial({map:longpicMap,transparent:true,side:THREE.DoubleSide});
        var mesh=new THREE.Mesh(cylinderG,cylinderM);
        mesh.scale.x=-1;
        mesh.material.opacity=0;
        mesh.renderOrder=999;

        pc.gp=new THREE.Group();
        pc.gp.add(mesh);
        pc.gp.scale.x=pc.gp.scale.y=2;

        var rotation=mesh.rotation.y;
        var rotation2=0;

        var bgG=new THREE.PlaneGeometry(1136,740,1,1);
        var bgM=new THREE.MeshBasicMaterial({map:arr_texture[11],transparent:true,side:THREE.DoubleSide});
        var bg=new THREE.Mesh(bgG,bgM);
        bg.material.opacity=0.0;

        // bg.material.opacity=0;
        bg.position.z=4500;
        bg.scale.x=10.5;
        bg.scale.y=10.5;


        var picG=new THREE.PlaneGeometry(59*10,59*10,1,1);
        var picM=new THREE.MeshBasicMaterial({map:tip_textrue,transparent:true,side:THREE.DoubleSide});
        var pic7=new THREE.Mesh(picG,picM);
        pic7.position.set(-0,-1600,-1800);
        pic7.material.opacity=0;

        var raycasterMouse  = new THREE.Raycaster();
        var mouse = new THREE.Vector2();
        // scene.add(bgPurple);

        pc.start=function(){
            // scene.add(mesh);
            document.dispatchEvent(new CustomEvent('CameraChange'));

            scene.add(pc.gp);
            scene.add(bg);
            scene.add(pic7);

            ccbl=true;

            setTimeout(function(){
                TweenMax.to(camera.position,1,{z:-100,ease:Cubic.easeOut});
                TweenMax.to(bg.position,2,{z:-1550,ease:Cubic.easeOut});

                TweenMax.to(mesh.material,2,{delay:0.5,opacity:1,ease:Cubic.easeOut});
                TweenMax.to(bg.material,1,{opacity:1,ease:Cubic.easeOut});


                mesh.material.blending="SubtractiveBlending";
                bg.renderOrder=5;
                mesh.renderOrder=10;
                renderer.setClearColor(0xffffff);
            })
            var bl=false;
            var xx=0;

            document.addEventListener("mousedown",MD);
            function MD(e){
                xx=e.clientY;
                bl=true;

                mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
                mouse.y = -(event.clientY / window.innerHeight) * 2 + 1;
                if (e.type == 'mousedown') {
                    var IO = [pic7];
                    raycasterMouse.setFromCamera(mouse, camera);
                    var intersectsStart = raycasterMouse.intersectObjects(IO, true);//
                    if (intersectsStart[0]) {
                        if(pic7.material.opacity!=0)
                        {
                            pic7.material.opacity=0;
                            scene.remove(pic7);
                        }

                        document.dispatchEvent(new CustomEvent("FinalPage"));
                    }
                }
            }
            document.addEventListener("mouseup",MU);
            function MU(e){
                console.log("mouseup");
                var balabl=xx-e.clientY;
                bl=false;
                console.log(mesh.rotation.y);
                var zaijian=false;
                if(mesh.rotation.y>4||mesh.rotation.y<-4){
                    // document.removeEventListener("mouseup",MU);
                    // document.removeEventListener("mousedown",MD);
                    // document.removeEventListener("mousemove",MM);
                    // setTimeout(function(){
                        // document.dispatchEvent(new CustomEvent("FinalPage"));
                    // },1000)
                    if(!bool_handShow)
                    {
                        bool_handShow=true;
                        TweenMax.to(pic7.material, 0.7, {delay: 0, opacity: 1, ease: Cubic.easeOut});
                    }

                }
                if(balabl<0) {
                    TweenMax.to(mesh.rotation, 1, {y: mesh.rotation.y + 1*balabl*0.01, ease: Cubic.easeOut});
                }
                else{
                    TweenMax.to(mesh.rotation, 1, {y: mesh.rotation.y + 1*balabl*0.01, ease: Cubic.easeOut});
                }
            }
            document.addEventListener("mousemove",MM);
            function MM(e){
                // console.log("mousemove");
                if(bl){
                    mesh.rotation.y+=(xx-e.clientY)*0.0001;
                }
            }

            document.addEventListener("touchstart",TS);
            function TS(e){
                // event.preventDefault();
                // console.log("mousedown");
                xx=e.touches[0].pageY;
                bl=true;

                mouse.x = (e.touches[0].pageX / window.innerWidth) * 2 - 1;
                mouse.y = -(e.touches[0].pageY / window.innerHeight) * 2 + 1;
                if (e.type == 'touchstart') {
                    var IO = [pic7];
                    raycasterMouse.setFromCamera(mouse, camera);
                    var intersectsStart = raycasterMouse.intersectObjects(IO, true);//
                    if (intersectsStart[0]) {
                        if(pic7.material.opacity!=0)
                        {
                            pic7.material.opacity=0;
                            scene.remove(pic7);
                        }
                        document.dispatchEvent(new CustomEvent("FinalPage"));
                    }
                }
            }
            document.addEventListener("touchend",TE);
            function TE(e){
                // alert('123123');
                event.preventDefault();
                // console.log("mouseup");
                var balabl=touchX;
                // alert(balabl);
                bl=false;
                if(mesh.rotation.y>4||mesh.rotation.y<-4){
                    // document.removeEventListener("touchend",TE);
                    // document.removeEventListener("touchstart",TS);
                    // document.removeEventListener("touchmove",TM);
                    // setTimeout(function(){
                        // document.dispatchEvent(new CustomEvent("FinalPage"));
                    // },1000)
                    if(!bool_handShow)
                    {
                        bool_handShow=true;
                        TweenMax.to(pic7.material, 0.7, {delay: 0, opacity: 1, ease: Cubic.easeOut});
                    }
                }
                if(balabl<0) {
                    TweenMax.to(mesh.rotation, 1, {y: mesh.rotation.y + 1*balabl*0.001, ease: Cubic.easeOut});
                }
                else{
                    TweenMax.to(mesh.rotation, 1, {y: mesh.rotation.y + 1*balabl*0.001, ease: Cubic.easeOut});
                }
            }
            document.addEventListener("touchmove",TM);
            function TM(e){
                event.preventDefault();
                // console.log("mousemove");
                if(bl){

                    mesh.rotation.y+=(xx-e.touches[0].pageY)*0.00003;
                    touchX=xx-e.touches[0].pageY;
                }
            }


            // window.addEventListener("deviceorientation", orientationHandler, false);
            // function orientationHandler(event) {
            //     aa = event.alpha;
            //     bb = event.beta;
            //     gg = event.gamma;
            //     if (Math.abs(bb) < 360 && camera != null) {
            //         mesh.rotation.y =Math.PI/180* bb*0.01;
            //     }
            // }



        }
        pc.end=function(){
            setTimeout(function () {
                var evt=new CustomEvent('CameraChange2');
                document.dispatchEvent(evt);
            },1000)


            TweenMax.to(mesh.material,1,{opacity:0,ease:Cubic.easeOut});
            TweenMax.to(bg.material,1,{opacity:0,ease:Cubic.easeOut});
            TweenMax.to(bg.scale,1,{x:5,y:5,ease:Cubic.easeOut});
            TweenMax.to(mesh.scale,0.01,{delay:1,x:0,y:0,ease:Cubic.easeOut});
            TweenMax.to(pic7.material,0.01,{opacity:0,ease:Cubic.easeOut});



            TweenMax.to(camera.position,5,{delay:1,z:1000});
        }
        pc.DoIt=function(){
            bg.rotation.z+=0.01;
        }
        return pc;
    }
}